package com.imm.da.main.launch

import android.util.Log
import com.alibaba.fastjson2.JSON
import com.imm.da.base.vm.BaseViewModel
import com.imm.da.bean.ImgParam
import com.imm.da.bean.ImgResult
import com.imm.da.utils.LogHelper
import com.imm.net.http.base.BaseHttpUrlFactory
import java.net.URL

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2024/11/19
 */
class AppLaunchViewModel: BaseViewModel() {

    /**
     * 定位成功之后，查询背景图
     * @param city 定位后的城市
     */
    fun query(
        param: ImgParam,
        success: (path: String) -> Unit
    ){
        request(
            query = {
                JSON.parseObject(BaseHttpUrlFactory.openConnection(
                    "GET",
                    url = {
                        val url = "https://image.so.com/j?q=${param.q}&sn=1&pn=1&width=411&height=731"
                        LogHelper.v("url: $url")
                        URL("https://image.so.com/j?q=${param.q}&sn=1&pn=1")
                    }
                ), ImgResult::class.java).list?.get(0)?.img
            },
            success = { imgUrl ->
                imgUrl?.let {
                    success(it)
                }?: LogHelper.e("img url can not be null")
            },
            failure = {
                Log.v("OkHttp", "query failure: $it")
            }
        )
    }

}